<?php
namespace Manage\Controller;

class DlyController extends BaseController {

    public function index(){
        $wl = M('b2c_dlycorp')->select();
        $this->assign('wlist',$wl);
        $this->display();
    }

    public function dlyDelete(){
        //删除一条数据
            $id = $_POST['id'];
         //判断是否有店铺使用该物流
           $find =M('b2c_store_dlycorp sd')
               ->join("left join ".C('DB_PREFIX')."webpos_local_store ls on ls.local_id = sd.local_id ")
               ->field('ls.local_name')
               ->where(['corp_id'=>$id])
               ->find();


            if(!empty($find)){
                $this->ajaxReturn(array('msg'=>'删除失败，门店！'.$find['local_name'].'在使用该物流','status'=>0));
            }
            $res = M('b2c_dlycorp')->where(['corp_id'=>$id])->delete();

            if($res){
                $this->ajaxReturn(array('msg'=>'删除成功！','status'=>1));
            }else{
                $this->ajaxReturn(array('msg'=>'删除失败！','status'=>0));
            }
    }

    public function DlyInfoEdit(){
        $id = I('id' , '', htmlentities());
        $info = M('b2c_dlycorp')->where(['corp_id'=>$id])->find();

        $this->assign('info',$info);
        $this->display('/Dly/addCorp');

    }

    public function dlyInfoAdd(){
        //添加物流信息管理

            $data = $_POST;
            if($data['corp_id']!=''){

                //编辑
                if(empty($data['name'])){
                    $this->error('请填写物流公司名称');
                }
                $res = M('b2c_dlycorp')->save($data);

                if($res){
                    $this->success('修改成功', '/manage/Dly/index.html');
                    exit;
                }else{
                    $this->success('修改失败', '/manage/Dly/index.html');
                    exit;
                }

            }else{
                //增加
                if (!empty($data)) {

                    if(empty($data['name'])){
                        $this->error('请填写物流公司名称');
                    }

                    $res = M('b2c_dlycorp')->data($data)->add();


                    if($res){
                        $this->success('添加成功', '/manage/Dly/index.html');
                        exit;
                    }else{
                        $this->success('添加失败', '/manage/Dly/index.html');
                        exit;
                    }

                }

            }

            $this->display('/Dly/addCorp');
    }


}